package com.opdar.athena.web.service;

import com.opdar.athena.base.RedisSessionManager;
import com.opdar.athena.entities.FunctionEntity;
import com.opdar.athena.entities.UserEntity;
import com.opdar.athena.mapper.FunctionMapper;
import com.opdar.platform.core.base.Context;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by shiju on 2016/12/9.
 */
@Service
public class FunctionService {
    @Autowired
    FunctionMapper functionMapper;
    @Autowired
    ApplicationContext applicationContext;

    public List<FunctionEntity> findSidebar(){
        List<FunctionEntity> list = new LinkedList<FunctionEntity>();
        String tokrn = (String) Context.getRequest().getSession().getAttribute("token");
        if(tokrn != null){
            RedisSessionManager sessionManager = applicationContext.getBean(RedisSessionManager.class);
            UserEntity usersEntity = sessionManager.get(tokrn);
            if(usersEntity != null){
                FunctionEntity t = new FunctionEntity();
                t.setModule("dashboard");
                t.setRoleId(usersEntity.getRole().equalsIgnoreCase("ADMIN")?"1":"2");
                t.setVaild(0);

                list = functionMapper.selectList(t);
            }
        }
        return list;
    }

}
